package com.cssl.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.client.BookinformationClient;
import com.cssl.pojo.*;
import com.cssl.service.BookinformationService;
import com.cssl.service.BooksmanagementService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;
import java.util.List;

@RestController
public class BookinformationController {
    @Autowired
    private BookinformationService infoservice;

    @Autowired
    private BookinformationClient bclient;



    @Autowired
    private BooksmanagementService booksmanagementService;


    @PostMapping("/pageall")
    public String pageall(String zhi,Integer dqy,Integer size){
        Page<Bookinformation> page = new Page<>(dqy,size);
        if (zhi==null){
            System.out.println(11111);
            Page<Bookinformation> page1 = infoservice.page(page,new QueryWrapper<Bookinformation>().orderByDesc("id"));
            for (Bookinformation boo:page.getRecords()) {
                Published p= bclient.selpname(boo.getPid());
                boo.setPublished(p);
                Booklevel b= bclient.selbname(boo.getBid());
                boo.setBooklevel(b);
                Location l= bclient.selwei(boo.getLid());
                boo.setLocation(l);
                Integer count = booksmanagementService.count(new QueryWrapper<Booksmanagement>().eq("sid",boo.getId()));
                boo.setJiechu(count);
                Integer countshu = infoservice.count();
                boo.setCountbenshu(countshu);
            }
        }else{
            System.out.println(22222);
            Page<Bookinformation> page1 = infoservice.page(page,new QueryWrapper<Bookinformation>().like("Callnumber",zhi).or().like("author",zhi).or().like("Title",zhi));
            for (Bookinformation boo:page.getRecords()) {
                Published p= bclient.selpname(boo.getPid());
                boo.setPublished(p);
                Booklevel b= bclient.selbname(boo.getBid());
                boo.setBooklevel(b);
                Location l= bclient.selwei(boo.getLid());
                boo.setLocation(l);
                Integer count = booksmanagementService.count(new QueryWrapper<Booksmanagement>().eq("sid",boo.getId()));
                boo.setJiechu(count);
                Integer countshu = infoservice.count(new QueryWrapper<Bookinformation>().like("Callnumber",zhi).or().like("author",zhi).or().like("Title",zhi));
                boo.setCountbenshu(countshu);
            }
        }


        String str = JSON.toJSONStringWithDateFormat(page,"yyyy-MM-dd");
        return str;
    }

    @PostMapping("/loads")
    public Bookinformation load(){
        Bookinformation bf=new Bookinformation();
        bf.setPublisheds(bclient.publishedall());
        bf.setLocations(bclient.locationall());
        bf.setBooklevels(bclient.booklevelall());
        return bf;
    }

    @PostMapping("/issuo")
    public boolean issuo(String Callnumber) {
        return infoservice.is(Callnumber);
    }
    @RequestMapping("/Callnumberselect")
    public String Callnumberselect(String Callnumber){
        System.out.println(222);
        Bookinformation bookinformation =  infoservice.getOne(new QueryWrapper<Bookinformation>().eq("Callnumber",Callnumber));
        Booklevel booklevel =  bclient.selbname(bookinformation.getBid());
        Published published =  bclient.selpname(bookinformation.getPid());
        bookinformation.setBooklevel(booklevel);
        bookinformation.setPublished(published);
        String str = JSON.toJSONString(bookinformation);
        return str;
    }

    @RequestMapping("/loadselect")
    public String loadselect(String zhi,Integer dqy,Integer size){
        Page<Bookinformation> page = new Page<>(dqy,size);
        if(zhi==null){
            Page<Bookinformation> page1 = infoservice.page(page,new QueryWrapper<Bookinformation>());
            for (Bookinformation boo:page.getRecords()) {
                Published p= bclient.selpname(boo.getPid());
                boo.setPublished(p);
            }
            String str = JSON.toJSONString(page);
            return str;
        }
        Page<Bookinformation> page1 = infoservice.page(page,new QueryWrapper<Bookinformation>().like("Callnumber",zhi).or().like("author",zhi).or().like("Title",zhi));
        for (Bookinformation boo:page.getRecords()) {
            Published p= bclient.selpname(boo.getPid());
            boo.setPublished(p);
        }
        String str = JSON.toJSONString(page);
        return str;
    }

    @GlobalTransactional
    @RequestMapping("/insertb")
    public boolean addbook(Bookinformation bf){
        /*return infoservice.addbook(bf)>0;*/
        if(infoservice.addbook(bf)>0){
           Booklevel booklevel =  bclient.selbname(bf.getBid());
           booklevel.setBshu(booklevel.getBshu()+1);
           boolean bl= bclient.updid(booklevel);
           Location location= bclient.selwei(bf.getLid());
           location.setShu(location.getShu()+1);
           boolean lt= bclient.updlid(location);
           if(bl==true&&lt==true){
               return true;
           }else{
               return false;
           }
        }
        return false;
    }

    @PostMapping("/bookselectbyid")
    public Bookinformation bookselectbyid(Integer id){
        return infoservice.bookselectbyid(id);
    }

    @GlobalTransactional
    @PostMapping("/updateb")
    public boolean updateb(Bookinformation bf){
        Bookinformation bookinformation =  infoservice.getOne(new QueryWrapper<Bookinformation>().eq("Callnumber",bf.getCallnumber()));
        Booklevel booklevel =  bclient.selbname(bookinformation.getBid());
        if (!(booklevel.getBid()==bf.getBid())){
            booklevel.setBshu(booklevel.getBshu()-1);
            bclient.updid(booklevel);
        }
        Location location= bclient.selwei(bookinformation.getLid());
        if(location.getLid()!=bf.getLid()){
            location.setShu(location.getShu()-1);
            bclient.updlid(location);
        }
        if(infoservice.updbook(bf)>0){//然后再改
            booklevel =  bclient.selbname(bf.getBid());
            booklevel.setBshu(booklevel.getBshu()+1);
            boolean bl=bclient.updid(booklevel);
            location= bclient.selwei(bf.getLid());
            location.setShu(location.getShu()+1);
            boolean lt= bclient.updlid(location);
            if(bl==true&&lt==true){
                return true;
            }
        }
        return false;
    }

    @GlobalTransactional
    @PostMapping("/shanc")
    public boolean shanc(String Callnumber){
        Bookinformation bookinformation =  infoservice.getOne(new QueryWrapper<Bookinformation>().eq("Callnumber",Callnumber));
        Booklevel booklevel =  bclient.selbname(bookinformation.getBid());
        booklevel.setBshu(booklevel.getBshu()-1);
        Location location= bclient.selwei(bookinformation.getLid());
        location.setShu(location.getShu()-1);
        if(bclient.updid(booklevel)==true&&bclient.updlid(location)==true){
            int is= infoservice.getBaseMapper().delete(new QueryWrapper<Bookinformation>().eq("Callnumber",Callnumber));
            if (is>0){
                return true;
            }else {
                return false;
            }
        }
        return  false;
    }

}
